package view.inventory.tip
{
import core.Language;

import flash.text.TextField;

import framework.view.components.Component;
import framework.view.components.tip.ITip;

import mx.utils.StringUtil;

import values.item.IItemValue;

/**
 * ItemValueTip
 * @author jliang
 *
 */
public class InventoryItemTip extends Component implements ITip
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function InventoryItemTip()
	{
		super();
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	protected var textPadding:Number = 0;
	
	protected var maxTipWidth:Number = 260;
	
	protected function get textField():TextField
	{
		return skin ? skin.label : null;
	}
	
	protected function get textWidthOffset():Number
	{
		return 4;
	}
	
	protected function get textHeightOffset():Number
	{
		return 6;
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	protected var vo:IItemValue;
	
	public function get data():*
	{
		return vo;
	}
	
	public function set data(value:*):void
	{
		vo = value;
		
		var content:String = StringUtil.substitute(Language.getText("@common_item_tip_content"),  
																			vo.template.name,
																			vo.template.desc);		
		setText(content);
	}
	//==========================================================================
	//  Overridden methods: Component
	//==========================================================================
	override protected function get skinReference():String
	{
		return "swf.ui.CommonTipSkin";
	}
	
	override public function initialize():void
	{
		super.initialize();
		
		textPadding = textField.x;
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================
	public function setText(value:String):void
	{
		reset();
		
		if (textField)
		{
			textField.htmlText = value;
		}
		resize();
	}
	
	protected function reset():void
	{
		textField.text = "";
		
		textField.width = maxTipWidth;
		textField.height = textField.textHeight + textHeightOffset;
	}
	
	protected function resize():void
	{
		var tw:Number = textField.textWidth + textWidthOffset;
		var th:Number = textField.textHeight + textHeightOffset;
		
		textField.width = tw;
		textField.height = th;
		
		skin.bg.width = tw + textPadding * 2;
		skin.bg.height = th + textPadding * 2;
	}
}
}




